package ltd.newbee.mall.service.impl;


import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.dao.IPurchaseInfoDAO;
import ltd.newbee.mall.dao.NewBeeMallGoodsMapper;
import ltd.newbee.mall.dto.PurchaseNewDTO;
import ltd.newbee.mall.entity.NewbeeMallPurchaseInfo;
import ltd.newbee.mall.entity.NewbeeMallSupplierInfo;
import ltd.newbee.mall.service.IPurchaseInfoService;
import ltd.newbee.mall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/**
 * @ClassName: PurchaseInfoServiceImpl
 * @Description: 采购信息Service接口的实现类
 * @Author: Emily
 * @Date: 2021-09-03 15:15
 * @Version 1.0
 */
@Service
@Transactional
public class PurchaseInfoServiceImpl implements IPurchaseInfoService
{
    @Autowired
    private IPurchaseInfoDAO purchaseInfoDAO;

    @Autowired
    private NewBeeMallGoodsMapper productService;

    @Override
    public Optional<NewbeeMallPurchaseInfo> findById(String purchaseId)
    {
        return purchaseInfoDAO.findById(purchaseId);
    }

    @Override
    public Page<NewbeeMallPurchaseInfo> findAll(Pageable pageable) {
        return purchaseInfoDAO.findAll(pageable);
    }

    @Override
    public Page<NewbeeMallPurchaseInfo> findBySupplierId(String supplierId, Pageable pageable) {
        return purchaseInfoDAO.queryBySupplierId(supplierId,pageable);
    }

    @Override
    public String save(NewbeeMallPurchaseInfo purchaseInfo) {
//        PurchaseNewDTO purchaseNewDTO = new PurchaseNewDTO();
//        purchaseNewDTO.setGoodsId(purchaseInfo.getGoodsId());
//        purchaseNewDTO.setProductQuantity(purchaseInfo.getPurchaseQuantity());
//        purchaseNewDTO.setImportPrice(purchaseInfo.getImportPrice());
//
//        purchaseNewDTO.setSupplierId(purchaseInfo.getSupplierId());
//
//        productService.addStock(purchaseInfo.getGoodsId(),purchaseInfo.getPurchaseQuantity());
//        return purchaseInfoDAO.save(purchaseInfo);

        try
        {
            PurchaseNewDTO purchaseNewDTO = new PurchaseNewDTO();
        purchaseNewDTO.setGoodsId(purchaseInfo.getGoodsId());
        purchaseNewDTO.setProductQuantity(purchaseInfo.getPurchaseQuantity());
        purchaseNewDTO.setImportPrice(purchaseInfo.getImportPrice());

        purchaseNewDTO.setSupplierId(purchaseInfo.getSupplierId());

//        productService.addStock(purchaseInfo.getGoodsId(),purchaseInfo.getPurchaseQuantity());
            Long goodsId = purchaseInfo.getGoodsId();
            Integer goodsCount = purchaseInfo.getPurchaseQuantity();
            if (!productService.addStock(goodsId, goodsCount)) {
                throw new RuntimeException("商品货品库存增加失败");
            }
            purchaseInfoDAO.save(purchaseInfo);
            return ServiceResultEnum.SUCCESS.getResult();
        }catch (Exception e)
        {
            return ServiceResultEnum.DB_ERROR.getResult();
        }

    }

    @Override
    public PageResult getNewBeeMallPurchasePage(PageRequest pageRequest) {
        Page<NewbeeMallPurchaseInfo> purchaseInfoDAOInfoPageList = purchaseInfoDAO.findAll(pageRequest);
        List<NewbeeMallPurchaseInfo> newbeeMallPurchaseInfos = purchaseInfoDAOInfoPageList.getContent();

        int total = (int)purchaseInfoDAO.count();

        PageResult pageResult = new PageResult(newbeeMallPurchaseInfos, total, pageRequest.getPageSize(), pageRequest.getPageNumber() + 1);

        return  pageResult;
    }
}
